package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;
    /**
     * 查询所有课程
     */
    @GetMapping
    public Result query(CourseDto courseDto) {
        log.info("查询所有课程{}",courseDto);
        PageResult<Course> pageResult = courseService.query(courseDto);
        return Result.success(pageResult);
        }

    /**
     * 根据ID删除课程
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除课程{}", id);
        courseService.delete(id);
        return Result.success();

    }
    /**
     * 添加课程
     */

    @Log
    @PostMapping
    public Result add(@RequestBody Course course) {
        log.info("添加课程{}", course);
        courseService.add(course);
        return Result.success();
    }
    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        log.info("查询课程{}", id);
        Course course = courseService.selectById(id);
        return Result.success(course);
    }
    /**
     * 修改课程
     */

    @Log
    @PutMapping
    public Result updata(@RequestBody Course course) {
        log.info("修改课程{}", course);
        courseService.updata(course);
        return Result.success();
    }
    /**
     * 查询所有课程
     */
    @GetMapping("/list")
    public Result findAll(Course  course) {
        log.info("查询所有课程{}", course);
        List<Course> all = courseService.findAll();
        return Result.success(all);
    }
    /**
     * 根据学科查询课程
     */
    @GetMapping("/subject/{subject}")
    public Result findBySubject(@PathVariable Integer subject) {
        log.info("根据学科查询课程{}", subject);
        List<Course> subjectResult = courseService.findBySubject(subject);
        return Result.success(subjectResult);
    }

}

